<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\LoginNotice;
use App\Models\Base\ServerList;
use App\Models\Login\LoginNotice as Notice;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

class LoginNoticeController extends Controller
{
    use ModelForm;
    private static $header;

    public function __construct()
    {
        self::$header = trans('game.gm_tool');
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(self::$header);
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(self::$header);
            $content->description(trans('admin.new'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        ServerList::$soft = true;

        return Admin::grid(LoginNotice::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->content(trans('game.content'))->display(function ($content) {
                return str_limit($content, 15, '...');
            });
            $grid->subcribe(trans('game.signature'));
            $grid->title(trans('game.notice_time'));
            $grid->status(trans('game.status'))->display(function ($status) {
                return $status ? "<span class='label bg-green'>".trans('game.normal')."</span>" : "<span class='label bg-red'>".trans('game.lock')."</span>";
            });
            $grid->created_at(trans('admin.created_at'));
            $grid->updated_at(trans('admin.updated_at'));
            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(LoginNotice::class, function (Form $form) {
            $form->display('id', 'ID');

            $form->textarea('content', trans('game.content'))->rows(10)->rules('required|min:10');
            $form->text('subcribe', trans('game.signature'))->rules('required');
            $form->text('title', trans('game.notice_time'))->rules('required|min:3')->help('例：xxx年xx月xx日');
            $states = [
                'on' => ['value' => 1, 'text' => trans('game.normal'), 'color' => 'success'],
                'off' => ['value' => 0, 'text' => trans('game.lock'), 'color' => 'danger'],
            ];
            $form->switch('status', trans('game.status'))->states($states)->default(1);
            $form->display('created_at', trans('admin.created_at'));
            $form->display('updated_at', trans('admin.updated_at'));

            $form->saving(function () {
                $post = Input::post();
                //写入登录服数据表
                $ret = $this->insertDb($post);
                if ($ret) {
                    //发送给游戏
                    if (!$this->sendGameNotice()) {
                        return back()->with(admin_toastr(trans('admin.gs_communication_failed'), 'error'))->withInput();
                    }
                } else {
                    return back()->with(admin_toastr(trans('game.write_db_failed'), 'error'))->withInput();
                }
            });
        });
    }

    /**
     * [sendGameNotice 公告发送给游戏].
     *
     * @Author   王文凡
     * @DateTime 2018-04-17
     *
     * @version  1.0
     *
     * @param [type] $content  [内容]
     * @param [type] $subcribe [署名]
     * @param [type] $pub_data [时间]
     *
     * @return mixed
     */
    protected function sendGameNotice()
    {
        $server = ServerList::first();
        $options = '2;0;0';

        return RequestService::sendNotice($server->server_id, $options);
    }

    /**
     * 插入登陆库.
     *
     * @param $post
     *
     * @return bool
     */
    protected function insertDb($post)
    {
        $db = new Notice();
        $db->title = $post['title'];
        $db->content = $post['content'];
        $db->signature = $post['subcribe'];
        $db->time = time();
        if ($db->save()) {
            return true;
        }

        return false;
    }
}
